package com.mifengs.mall.floor;

import android.graphics.Paint;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.annotation.ItemProviderTag;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.mifengs.mall.R;
import com.mifengs.mall.adapter.TwoThreeItemAdapter;
import com.mifengs.mall.entity.AdvertBean;
import com.mifengs.mall.entity.FloorBean;
import com.mifengs.mall.util.ImageLoadUtil;
import com.mifengs.mall.util.ToIntentUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lynn on 2018/4/19.
 */
@ItemProviderTag(
        viewType = FloorListAdapter.TYPE_SEVEN,
        layout = R.layout.fragment_two_three_flo
)
public class FloorSevenItemProvider extends BaseItemProvider<FloorBean,BaseViewHolder> {

    private TwoThreeItemAdapter mTwoThreeItemAdapter;

    @Override
    public void convert(BaseViewHolder viewHolder, final FloorBean floorBean, int index) {
        final RecyclerView mRecyclerView = viewHolder.getView(R.id.discount_list);
        ImageView one_img = viewHolder.getView(R.id.one_img);
        ImageView two_img = viewHolder.getView(R.id.two_img);
        TextView des_text = viewHolder.getView(R.id.des_text);
        TextView pro_price_text = viewHolder.getView(R.id.pro_price_text);
        TextView pro_name = viewHolder.getView(R.id.pro_name);
        TextView normal_price_text = viewHolder.getView(R.id.normal_price_text);
        TextView original_price_text = viewHolder.getView(R.id.original_price_text);
        RelativeLayout des_rl_layout = viewHolder.getView(R.id.des_rl_layout);
        if(floorBean !=null){
            for(int i = 0;i<floorBean.getAdverts().size();i++ ){
                AdvertBean adverts = floorBean.getAdverts().get(i);
                switch (i){
                    case 0:
                        ImageLoadUtil.displayImage(mContext,adverts.getImg(),one_img);
                        des_text.setText(adverts.getText());
                        pro_price_text.setText("¥"+adverts.getPrice());
                        des_rl_layout.getBackground().setAlpha(150);
                        break;
                    case 1:
                        two_img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                        ImageLoadUtil.displayImage(mContext,adverts.getImg(),two_img);
                        pro_name.setText(adverts.getText());
                        normal_price_text.setText("¥"+adverts.getPrice());
                        original_price_text.setText("¥"+adverts.getCashwtf());
                        original_price_text.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                        original_price_text .getPaint().setAntiAlias(true);
                        break;
                }
            }

            one_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AdvertBean adverts = floorBean.getAdverts().get(0);
                    ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                }
            });
            two_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AdvertBean adverts = floorBean.getAdverts().get(1);
                    ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                }
            });

            List<AdvertBean> tempAdvertBeanList = floorBean.getAdverts();
            final List<AdvertBean> advertBeanList = new ArrayList<AdvertBean>();
            for(int i = 2;i<tempAdvertBeanList.size();i++){
                AdvertBean advertBean = tempAdvertBeanList.get(i);
                advertBeanList.add(advertBean);
                if(advertBeanList.size() ==3){
                    break;
                }
            }
//            mRecyclerView.addItemDecoration(new DividerGridItemDecoration(mContext));
            mRecyclerView.setLayoutManager(new GridLayoutManager(mContext,3));
            mRecyclerView.setNestedScrollingEnabled(false);
            mTwoThreeItemAdapter= new TwoThreeItemAdapter(R.layout.item_two_three_floor,advertBeanList,mContext);
            mRecyclerView.setAdapter(mTwoThreeItemAdapter);
            mTwoThreeItemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    AdvertBean adverts = advertBeanList.get(position);
                    ToIntentUtil.toCommonIntent(adverts.getActionUrl(),mContext);
                }
            });
        }
    }

    @Override
    public void onClick(BaseViewHolder baseViewHolder, FloorBean floorBean, int i) {

    }

    @Override
    public boolean onLongClick(BaseViewHolder baseViewHolder, FloorBean floorBean, int i) {
        return false;
    }
}
